//
//  StatusRetweetedView.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/21.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import UIKit
import SnapKit

class StatusRetweetedView: UIView {
    //  定义约束属性,保存当前视图的底部约束
    var bottomConstraints:Constraint?
    
    // 定义外部模型属性
    var retweetedStatus:Status?{
        didSet{
            
            retweetedLabel.text = "@" + (retweetedStatus?.user?.name ?? "") + (retweetedStatus?.text ?? "")
            
            pictureView.imageURLs = retweetedStatus?.imageURLs
            
            //卸载掉底部的约束
            self.bottomConstraints?.uninstall()
            if let urls = retweetedStatus?.imageURLs where urls.count > 0{
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
                })
                
                
            } else {
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMarigin).constraint
                })
                
                
                
                
            }
            
            
            
            
            
            
        }
        
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        backgroundColor = UIColor(white: 0.95, alpha: 1)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    private func setupUI(){
        addSubview(retweetedLabel)
        addSubview(pictureView)
        //添加约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMarigin)

        }
     
        
pictureView.snp_makeConstraints { (make) -> Void in
    
    make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMarigin)
    make.left.equalTo(retweetedLabel.snp_left)
        }
        
        //转发微博自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            
         self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMarigin).constraint
        }
        
        
        
        
    }
    
   
    
    
    
    
    //懒加载所有视图
    private lazy var retweetedLabel:UILabel = UILabel(title: "我是转发微博", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMarigin)
    private lazy var pictureView:StatusPictureView = StatusPictureView()

}
